/**
 * 将SVG转换为PNG图片并下载
 * @param {string} svgString - SVG字符串
 * @param {string} fileName - 下载的文件名
 * @param {number} scale - 放大比例，默认为2
 */
export const downloadSvgAsPng = (svgString, fileName = 'card', scale = 2) => {
  if (!svgString) return;
  
  const svgBlob = new Blob([svgString], {
    type: "image/svg+xml;charset=utf-8",
  });
  const svgUrl = URL.createObjectURL(svgBlob);
  
  const img = new Image();
  img.onload = () => {
    // 获取SVG宽高
    const parser = new DOMParser();
    const svgDoc = parser.parseFromString(svgString, 'image/svg+xml');
    const svgElement = svgDoc.documentElement;
    
    // 尝试获取宽高
    let width = parseFloat(svgElement.getAttribute('width') || '400');
    let height = parseFloat(svgElement.getAttribute('height') || '600');
    
    // 如果没有宽高但有viewBox
    const viewBox = svgElement.getAttribute('viewBox');
    if (viewBox && (!width || !height)) {
      const [, , vbWidth, vbHeight] = viewBox.split(' ').map(Number);
      width = width || vbWidth;
      height = height || vbHeight;
    }
    
    const canvas = document.createElement('canvas');
    canvas.width = width * scale;
    canvas.height = height * scale;
    const ctx = canvas.getContext('2d');
    
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    
    canvas.toBlob((blob) => {
      if (!blob) return;
      
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = `${fileName}.png`;
      a.click();
      
      URL.revokeObjectURL(url);
      URL.revokeObjectURL(svgUrl);
    }, 'image/png');
  };
  
  img.src = svgUrl;
};

/**
 * 将SVG复制到剪贴板
 * @param {string} svgString - SVG字符串 
 * @param {number} scale - 放大比例，默认为2
 * @returns {Promise<boolean>} - 是否复制成功
 */
export const copySvgToClipboard = async (svgString, scale = 2) => {
  if (!svgString) return false;
  
  try {
    const svgBlob = new Blob([svgString], {
      type: "image/svg+xml;charset=utf-8",
    });
    const svgUrl = URL.createObjectURL(svgBlob);
    
    const img = new Image();
    
    return new Promise((resolve) => {
      img.onload = async () => {
        // 获取SVG宽高
        const parser = new DOMParser();
        const svgDoc = parser.parseFromString(svgString, 'image/svg+xml');
        const svgElement = svgDoc.documentElement;
        
        // 尝试获取宽高
        let width = parseFloat(svgElement.getAttribute('width') || '400');
        let height = parseFloat(svgElement.getAttribute('height') || '600');
        
        // 如果没有宽高但有viewBox
        const viewBox = svgElement.getAttribute('viewBox');
        if (viewBox && (!width || !height)) {
          const [, , vbWidth, vbHeight] = viewBox.split(' ').map(Number);
          width = width || vbWidth;
          height = height || vbHeight;
        }
        
        const canvas = document.createElement('canvas');
        canvas.width = width * scale;
        canvas.height = height * scale;
        const ctx = canvas.getContext('2d');
        
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        
        try {
          const blob = await new Promise(resolve => 
            canvas.toBlob(resolve, 'image/png')
          );
          
          await navigator.clipboard.write([
            new ClipboardItem({ 'image/png': blob })
          ]);
          
          resolve(true);
        } catch (err) {
          console.error(err);
          resolve(false);
        } finally {
          URL.revokeObjectURL(svgUrl);
        }
      };
      
      img.onerror = () => {
        URL.revokeObjectURL(svgUrl);
        resolve(false);
      };
      
      img.src = svgUrl;
    });
  } catch (error) {
    console.error('Error:', error);
    return false;
  }
}; 